package com.ruoyi.shop.emporium.order.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.common.domain.DtsOrderStatus;
import com.ruoyi.mall.common.domain.DtsRefundzTrace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.common.mapper.DtsOrderMapper;
import com.ruoyi.mall.common.domain.DtsOrder;
import com.ruoyi.shop.emporium.order.service.IDtsOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class DtsOrderServiceImpl implements IDtsOrderService
{
    @Autowired
    private DtsOrderMapper dtsOrderMapper;

    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public DtsOrder selectDtsOrderById(Long id)
    {
        return dtsOrderMapper.selectDtsOrderById(id);
    }

    /**
     * 查询订单管理列表
     *
     * @param dtsOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<DtsOrder> selectDtsOrderList(DtsOrder dtsOrder)
    {
//        List<DtsOrder> list = dtsOrderMapper.selectDtsOrderList(dtsOrder);
//        List<DtsOrderStatus> list1 = dtsOrderMapper.selectDtsOrderStatusList();
//        list.get(0).setOrderStatusList(list1);
        return dtsOrderMapper.selectDtsOrderList(dtsOrder);
    }

    @Override
    public List<DtsOrderStatus> selectDtsOrderStatusList() {
        return dtsOrderMapper.selectDtsOrderStatusList();
    }

    /**
     * 新增订单管理
     *
     * @param dtsOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertDtsOrder(DtsOrder dtsOrder)
    {
        return dtsOrderMapper.insertDtsOrder(dtsOrder);
    }

    /**
     * 修改订单管理
     *
     * @param dtsOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateDtsOrder(DtsOrder dtsOrder)
    {
        dtsOrder.setUpdateTime(DateUtils.getNowDate());
        return dtsOrderMapper.updateDtsOrder(dtsOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteDtsOrderByIds(Long[] ids)
    {
        return dtsOrderMapper.deleteDtsOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteDtsOrderById(Long id)
    {
        return dtsOrderMapper.deleteDtsOrderById(Math.toIntExact(id));
    }

    @Override
    public DtsRefundzTrace selectRefund(String OrderSn) {
        return dtsOrderMapper.selectRefund(OrderSn);
    }

    @Override
    public int approveRefund(DtsRefundzTrace dtsRefundzTrace) {
        dtsRefundzTrace.setApproveTime(new Date());
        return dtsOrderMapper.updateRefundStatus(dtsRefundzTrace);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务注解
    public int completeRefund(DtsRefundzTrace dtsRefundzTrace) {

        //同意退款后将订单表状态改为已退款
        DtsOrder dtsOrder = new DtsOrder();
        dtsOrder.setId(dtsRefundzTrace.getOrderId());
        dtsOrder.setOrderStatus("203");
        dtsOrderMapper.updateDtsOrder(dtsOrder);

        dtsRefundzTrace.setStatus(3); // 3 表示已完成
        dtsRefundzTrace.setRefundTime(new Date());
        return dtsOrderMapper.updateRefundStatus(dtsRefundzTrace);
    }
}
